package com.xd.study.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;

import com.xd.study.R;

/**
 * Created by DGF on 2015/5/12.
 * 上拉刷新ListView
 */
public class UpLoadListView extends ListView implements AbsListView.OnScrollListener {

    /**
     * 加载更多数据的回调接口
     * 在Activity中实现该接口
     */
    public interface ILoadListener{
        void onLoad();
    }

    /**
     * 加载数据的接口
     */
    private ILoadListener loadListener = null;
    public void setLoadListener(ILoadListener listener){
        this.loadListener = listener;
    }

    /**
     * 是否滚动到底部
     */
    private boolean bFooter = false;

    /**
     * 是否正在加载
     */
    private boolean bLoading = false;

    /**
     * 底部布局
     */
    private View footer;

    public UpLoadListView(Context context) {
        super(context);

        init(context);
    }

    public UpLoadListView(Context context, AttributeSet attrs) {
        super(context, attrs);

        init(context);
    }

    public UpLoadListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        init(context);
    }

    /**
     * 初始化
     */
    private void init(Context context){
        /**
         * 添加底部布局
         */
        LayoutInflater inflater = LayoutInflater.from(context);
        footer = inflater.inflate(R.layout.listview_footer, null);
        footer.findViewById(R.id.up_load_layout).setVisibility(View.GONE);
        this.addFooterView(footer);

        /**
         * 设置滚动监听
         */
        this.setOnScrollListener(this);
    }

    /**
     * 滚动到底部时，显示底部布局，加载数据
     * @param view
     * @param scrollState
     */
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if (bFooter && scrollState == SCROLL_STATE_IDLE){
            if (!bLoading){
                bLoading = true;

                /**
                 * 显示底部布局
                 */
                footer.findViewById(R.id.up_load_layout).setVisibility(View.VISIBLE);

                /**
                 * 加载数据
                 */
                if (loadListener != null){
                    loadListener.onLoad();
                }
            }
        }
    }

    /**
     * 判断是否滚到底部
     * @param view
     * @param firstVisibleItem
     * @param visibleItemCount
     * @param totalItemCount
     */
    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        Log.i("tag",
                "firstVisibleItem: " + firstVisibleItem +
                " visibleItemCount: " + visibleItemCount +
                " totalItemCount: " + totalItemCount);
        bFooter = (firstVisibleItem + visibleItemCount == totalItemCount);
    }

    /**
     * 加载完毕
     */
    public void loadCompleted(){
        bLoading = false;

        /**
         * 隐藏底部布局
         */
        footer.findViewById(R.id.up_load_layout).setVisibility(View.GONE);
    }
}
